function copyTable (tableName)
  local copiedMap

  local function copyTable2 (oldTable)
    if (type(oldTable) ~= "table") then
      return oldTable
    elseif (copiedMap[oldTable]) then  -- avoid recursion / repetition
      return copiedMap[oldTable]
    else
      local newTable = {}
      copiedMap[oldTable] = newTable

      for name, value in pairs(oldTable) do
        newTable[name] = copyTable2(value)
        --newTable[copyTable2(name)] = copyTable2(value)  --table as index
      end	
      
      --return newTable
      return setmetatable(newTable, getmetatable(oldTable))  --copy metatable
    end	
  end

  
  copiedMap = {}
  return copyTable2 (tableName)
end


function inheritTable (inheritedTable, newValues)
end


function pairsByKeys (tableName)
  local sortedTable = {}
  for key in pairs(tableName) do
    sortedTable[#sortedTable + 1] = key
  end
  table.sort(sortedTable)
  
  local i = 0
  local v
  local iterator = function()
    i = i + 1
    v = sortedTable[i]
    return v, (v and tableName[v])
  end
  return iterator
end


function alignValues (tableName, keyName)
  local maxLength = 0
  local length  
  for name, value in pairs(tableName) do
    length = string.len(value[keyName])
    if (length > maxLength) then maxLength = length end
  end
  
  local formatString = "%-" .. maxLength .. "s"  
  for name, value in pairs(tableName) do
    value[keyName] = formatString:format(value[keyName])
  end 
end

-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------

--[[
--deep not safe with circular tables! defaults To false
function Spring.Utilities.CopyTable(tableToCopy, deep)
  local copy = {}
  for key, value in pairs(tableToCopy) do
    if (deep and type(value) == "table") then
      copy[key] = CopyTable(value, true)
    else
      copy[key] = value
    end
  end
  return copy
end


-- modifies primary directly; if you don't want to do that then make a copy and pass that
function Spring.Utilities.MergeTable(primary, secondary, deep)
	for i, v in pairs(secondary) do
		-- key not used in primary, assign it the value at same key in secondary
		if not primary[i] then
			if (deep and type(v) == "table") then
				primary[i] = CopyTable(v, true)
			else
				primary[i] = v
			end
		-- values at key in both primary and secondary are tables, merge those
		elseif type(primary[i]) == "table" and type(v) == "table"  then
			Spring.Utilities.MergeTable(primary[i], v, deep)
		end
	end
end

--unused
local function ModifyTable(original, modify)   -- Warning: circular table references lead to an infinite loop.
	for k, v in pairs(modify) do
		--Spring.Echo("Original entry: "..original[k])
		--Spring.Echo("Modifier entry: "..k)
		--if not (original and modify) then return end
		if (type(v) == "table") then
			ModifyTable(original[k], v)
		else
			original[k] = v
		end
	end
end




local function tableMerge(t1, t2, appendIndex)
	for k,v in pairs(t2) do
		if type(v) == "table" then
			if type(t1[k] or false) == "table" then
				tableMerge(t1[k] or {}, t2[k] or {}, appendIndex)
			else
				if type(k) == 'number' and appendIndex then
					t1[#t1+1] = v
				else
					t1[k] = v
				end
			end
		else
			if type(k) == 'number' and appendIndex then
				t1[#t1+1] = v
			else
				t1[k] = v
			end
		end
	end
	return t1
end


local function MergeTable(table1,table2)
  local ret = {}
  CopyTable(ret,table2)
  CopyTable(ret,table1)
  return ret
end--]]






